﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WROA.WEB.Models;
using System.IO;

namespace WROA.Helper
{
    public class FileRepository
    {
        public List<FileAttribute> GetAllFileDescription()
        {
            String FolderPath = HttpContext.Current.Server.MapPath("~/UpLoadFile");//取得Server上傳資料夾
            String[] files = Directory.GetFiles(FolderPath);//取得上傳資料夾中所有檔案
            List<FileAttribute> fileDescriptions = new List<FileAttribute>();
            int i = 1;
            foreach (String file in files)//循環取得檔案相關屬性資訊
            {
                FileInfo fileinfo = new FileInfo(file);
                fileDescriptions.Add(
                    new FileAttribute
                    {
                        id = i,
                        Name = fileinfo.Name,
                        Path = fileinfo.DirectoryName,
                        Size = fileinfo.Length,
                        Created = fileinfo.CreationTime
                    });
                i++;
            }
            return fileDescriptions;
        }

        public string ChangeWord(string fileNameStr)
        {
            Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = word.GetType();
            Microsoft.Office.Interop.Word.Documents docs = word.Documents;

            // 打开文件
            Type docsType = docs.GetType();

            //应当先把文件上传至服务器然后再解析文件为html
            string filePath = HttpContext.Current.Server.MapPath("~/UpLoadFile/" + fileNameStr + "");

            object fileName = filePath;

            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
            System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });

            // 转换格式，另存为html
            Type docType = doc.GetType();

            string filename = DateTime.Now.ToString("yyyyMMdd");

            //被转换的html文档保存的位置
            string ConfigPath = HttpContext.Current.Server.MapPath("~/html/" + filename + ".html");
            object saveFileName = ConfigPath;
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
            null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
            // 退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            //转到新生成的页面
            //string s = "<script langurage='javascript'>window.open('" + ConfigPath + "');</script>";
            string file = "http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port + "/html/" + filename + ".html";
            //string s = "<script langurage='javascript'>window.location='" + file + "';</script>";
            string s = "<script langurage='javascript'>window.open('" + file + "');</script>";
            return s;
        }
    }
}